<?php

namespace App\Models\Flaw;

use App\Models\Access\User\User;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class VulsDelay extends Model
{
    protected $table = 'vuls_delay';

    public $timestamps = true;

    protected $guarded = [];

    protected $fillable = [];

    /**
     * 申请
     * @param $request
     * @return bool
     * @throws \Exception
     */
    public static function apply ($request){
        $data = $request->input();
        $data['applyer']    = Auth::id();
        $data['delayed_at'] = Carbon::parse($data['delayed_at']);
        if (self::create($data)){

            return self::insertData($data);
        }
        throw new \Exception('网络繁忙，请稍后再试');
    }

    /**
     * 审批
     * @param $request
     * @return bool
     * @throws \Exception
     */
    public static function audit ($request){
        $row = self::where(['vuls_id'=>$request->vuls_id])->first();
        $data = $request->input();
        $data['agree'] = intval($request->agree);
        if(!empty($row) && $row->update($data)){

            return self::insertData($data,true);
        }
        throw new \Exception('审核失败');
    }

    /**
     * 重复申请
     * @param $request
     * @return bool
     * @throws \Exception
     */
    public static function reApply($request){
        $row = self::where(['vuls_id'=>$request->vuls_id])->first();
        $data = $request->input();
        $data['delayed_at'] = Carbon::parse($data['delayed_at']);
        $times = DB::table('vuls_delay_data')->where(['vuls_id'=>$data['vuls_id']])->count();
        $configBase = DB::table('sys_config')->where(['module'=>'base'])->first();
        $delayTimes = 0;
        $dtObj = isset($configBase->setting) ? json_decode($configBase->setting) : collect();
        if (isset($dtObj->delay_times)){

            $delayTimes = intval($dtObj->delay_times);
        }
        if($times > $delayTimes){
            throw new \Exception('超出最大申请次数');
        }
        if(!empty($row) && $row->update($data)){

            return self::insertData($data);
        }
        throw new \Exception('网络繁忙，请稍后再试');
    }

    /**
     * 延期时间不能小于系统默认的修复时间
     * @param int $vul_id
     * @param string $format
     * @return string
     */
    public static function getBeginTime($vul_id,$format='Y-m-d'){
        $ret = '';
        if ($vul_id){
            $vulInfo = Vuls::select(['level.term','vuls.check_at as send_time'])->where(['vuls.id'=>$vul_id])->join('level','vuls.level_id','=','level.id')->first();

            $ret = Carbon::parse($vulInfo->send_time)->addDay($vulInfo->term)->format($format);
        }
        return $ret;
    }

    /**
     * 延期申请审核对话内容
     * @param $vul_id
     * @return mixed
     */
    public static function getDelayContent (int $vul_id) {

        $dc = DB::table('vuls_delay_data as vda')->select([
            'vda.vuls_id',
            'vda.applyer',
            'vda.auditer',
            'vda.reason',
            'vda.created_at',
            'u.name'
        ])->join('users as u', function ($join) {
            return $join->on('u.id', '=', 'vda.applyer')->orOn('u.id', '=', 'vda.auditer');
        })->where(['vuls_id' => $vul_id])->where(function ($query) {
            $query->where('vda.applyer', '<>', 0)->orWhere('vda.auditer', '<>', 0);
        })->get();

        return $dc;
    }

    /**
     * 存申请审核对话内容
     * @param array $data
     * @param bool $audit
     * @return bool
     */
    public static function insertData(array $data,$audit = false){
        $delayContent = ['vuls_id'=>$data['vuls_id'],'reason'=>$data['reason'],'created_at'=>Carbon::now()];
        if ($audit){

            $delayContent['auditer'] = Auth()->id();
        } else{

            $delayContent['applyer'] = Auth()->id();
        }
        return DB::table('vuls_delay_data')->insert($delayContent);
    }

}
